package com.example.yuejianapp14.Activity.Activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.yuejianapp14.Activity.Fragment.ClubClassFragment;
import com.example.yuejianapp14.Activity.Fragment.ClubInfoFragment;
import com.example.yuejianapp14.Activity.Fragment.ClubVideoFragment;
import com.example.yuejianapp14.Activity.JavaBean.Success;
import com.example.yuejianapp14.Activity.Listener.ListListener;
import com.example.yuejianapp14.Activity.Model.CollectedModel;
import com.example.yuejianapp14.R;
import com.squareup.picasso.Picasso;

public class ClubActivity extends FragmentActivity implements View.OnClickListener{
        private ImageView back,img;
        private ImageButton club_collect;
        private TextView mclubinfo;
        private TextView mclubclass;
        private TextView mclubvideo;
        private TextView club_name;
        private Fragment mTab01;
        private Fragment mTab02;
        private Fragment mTab03;
        private Context mContext;
        private Intent intent;
        private SharedPreferences sp;
        String  type,typeid;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_club);
        initViews();
        initEvent();
        getData();
        setSelect(0);
        Intent intent = getIntent();
        intent.getStringExtra("clubid");
        typeid=intent.getStringExtra("clubid");


        club_name.setText(getIntent().getStringExtra("club_name"));
        Picasso.with(mContext).load("http://10.0.2.2/YueJian/Public/photo/club/"+getIntent().getStringExtra("img")).into(img);

        back.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });
        club_collect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CollectedModel collectedModel = new CollectedModel();
                collectedModel.Collect(sp.getString("uid",""),"1",typeid,collectListener);
            }
        });
    }
        private void initEvent()
        {
            mclubinfo.setOnClickListener(this);
            mclubclass.setOnClickListener(this);
            mclubvideo.setOnClickListener(this);

        }
    public void initViews(){
        back=(ImageView)findViewById(R.id.back);
        club_collect=(ImageButton)findViewById(R.id.club_collect) ;
        mclubinfo = (TextView) findViewById(R.id.clubinfo);
        mclubclass = (TextView) findViewById(R.id.clubclass);
        mclubvideo = (TextView) findViewById(R.id.clubvideo);
        img=(ImageView)findViewById(R.id.club_pic);
        club_name=(TextView)findViewById(R.id.club_name);
        sp=getSharedPreferences("uid",MODE_PRIVATE);
    }
        private void setSelect(int i)
        {
            FragmentManager fm = getSupportFragmentManager();
            FragmentTransaction transaction = fm.beginTransaction();
            hideFragment(transaction);

            switch (i)
            {
                case 0:
                    if (mTab01 == null)
                    {
                        mTab01 = new ClubInfoFragment();
                        transaction.add(R.id.id_content2, mTab01);
                    } else
                    {
                        transaction.show(mTab01);
                    }
                    break;
                case 1:
                    if (mTab02 == null)
                    {
                        mTab02 = new ClubClassFragment();
                        transaction.add(R.id.id_content2, mTab02);
                    } else
                    {
                        transaction.show(mTab02);
                    }
                    break;
                case 2:
                    if (mTab03 == null)
                    {
                        mTab03 = new ClubVideoFragment();
                        transaction.add(R.id.id_content2, mTab03);
                    } else
                    {
                        transaction.show(mTab03);
                    }
                    break;

                default:
                    break;
            }

            transaction.commit();
        }
        private void hideFragment(FragmentTransaction transaction)
        {
            if (mTab01 != null)
            {
                transaction.hide(mTab01);
            }
            if (mTab02 != null)
            {
                transaction.hide(mTab02);
            }
            if (mTab03 != null)
            {
                transaction.hide(mTab03);
            }

        }
        public void onClick(View v)
        {
            switch (v.getId())
            {
                case R.id.clubinfo:
                    setSelect(0);
                    break;
                case R.id.clubclass:
                    setSelect(1);
                    break;
                case R.id.clubvideo:
                    setSelect(2);
                    break;
                default:
                    break;
            }
        }

    ListListener<Success> collectListener=new ListListener<Success>() {
        @Override
        public void onResponse(Success a) {
            if(a.getSuccess().equals("1")){
                Toast.makeText(ClubActivity.this, "关注成功",Toast.LENGTH_SHORT).show();
                Intent intent = getIntent();
                typeid=intent.getStringExtra("clubid");
                CollectedModel collectedModel = new CollectedModel();
                collectedModel.CollectInfo(sp.getString("uid",""),"1",typeid,collectInfoListener);
            }
            else if (a.getSuccess().equals("0")){
                Toast.makeText(ClubActivity.this, "成功取消关注",Toast.LENGTH_SHORT).show();
                Intent intent = getIntent();
                typeid=intent.getStringExtra("clubid");
                CollectedModel collectedModel = new CollectedModel();
                collectedModel.CollectInfo(sp.getString("uid",""),"1",typeid,collectInfoListener);
            }
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(ClubActivity.this, "fail",Toast.LENGTH_SHORT).show();
        }
    };
    ListListener<Success> collectInfoListener=new ListListener<Success>() {
        @Override
        public void onResponse(Success a) {
            if(a.getSuccess().equals("1")){
                club_collect.setImageResource(R.drawable.collected);
            }
            else {
                club_collect.setImageResource(R.drawable.collect);
            }
        }
        @Override
        public void onFail(String msg) {
        }
    };
    private void getData() {
        Intent intent = getIntent();
        typeid=intent.getStringExtra("clubid");
        CollectedModel collectedModel = new CollectedModel();
        collectedModel.CollectInfo(sp.getString("uid",""),"1",typeid,collectInfoListener);

    }

}
